package com.android.dialer;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.telecom.Call;
import android.telecom.VideoProfile;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.android.dialer.remote.ActivityRemote;
import com.android.dialer.util.CallManager;
import com.android.incallui.Log;
import com.android.remote.RemoteAsyncCall;

import java.util.Timer;
import java.util.TimerTask;

@SuppressLint("NewApi")
public class TelecomActivity extends AppCompatActivity {
    private Timer timer = new Timer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_telecom);

        final Call call = CallManager.getInstance().getCall();

        final TextView phoneNum = (TextView) findViewById(R.id.phoneNum);
        final TextView duration = (TextView) findViewById(R.id.duration);

        final TimerTask task = new TimerTask() {
            long time = 0;

            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        duration.setText(timeFormat(time));
                    }
                });
                time += 1;
            }
        };
        final Call.Callback callback = new Call.Callback() {
            @Override
            public void onStateChanged(Call call, int state) {
                super.onStateChanged(call, state);
                Log.e("FK-HYL", "STATE:" + state);
                switch (state) {
                    case Call.STATE_ACTIVE:
                        duration.setText(R.string.zeroTime);
                        timer.schedule(task, 1000, 1000);
                        break;
                    case Call.STATE_DISCONNECTING:
                    case Call.STATE_DISCONNECTED:
                        task.cancel();
                        duration.setText("通话挂断");
                        call.disconnect();
                        call.unregisterCallback(this);
                        finish();
                        break;
                }
            }
        };
        call.registerCallback(callback);

        String num = call.getDetails().getHandle().getEncodedSchemeSpecificPart();
        int index = num.indexOf(' ');
        if (index != -1) {
            phoneNum.setText(num.substring(0, index));
        } else {
            phoneNum.setText(num);
        }

        final View fab = findViewById(R.id.fab);
        if (call.getState() == Call.STATE_CONNECTING) {
            fab.setVisibility(View.INVISIBLE);
        }
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (call.getState() == Call.STATE_RINGING) {
                    call.answer(VideoProfile.STATE_AUDIO_ONLY);
                }
                fab.setVisibility(View.INVISIBLE);
            }
        });
        findViewById(R.id.hangUp).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                call.disconnect();
                call.unregisterCallback(callback);
                finish();
            }
        });
        findViewById(R.id.wechat_invite).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("AD-HYL", "添加微信好友");
                ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                if (clipboard != null) {
                    clipboard.setPrimaryClip(ClipData.newPlainText("群名", phoneNum.getText()));
                }
                ActivityRemote remote = new ActivityRemote();
                remote.activity = "com.tencent.mm.plugin.fts.ui.FTSAddFriendUI";
                RemoteAsyncCall.remoteCall(remote);
            }
        });
    }

    private String timeFormat(long second) {
        long[] ls = new long[]{second / 3600, second % 3600 / 60, second % 60};
        StringBuilder builder = new StringBuilder();
        for (long l : ls) {
            if (l < 10) {
                builder.append('0');
            }
            builder.append(l).append(':');
        }
        return builder.deleteCharAt(builder.length() - 1).toString();
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            return false;
        }
        return super.dispatchKeyEvent(event);
    }
}